#include "sys_config.h"

/**
 * @brief 封装上传的数据
 * @note 获取全局变量中的各个外设及传感器状态
 * @param {*}
 * @return 0：成功，-1：失败
 */
int Json_Enc_Data(char *data)
{
    cJSON *root = NULL, *lamp = NULL, *beep = NULL, *fan = NULL, *environment = NULL, *services = NULL;
    cJSON *properties = NULL, *RGB_color = NULL;
    int ret = 0;

    // 组JSON数据
    root = cJSON_CreateObject(); // 创建一个对象
    services = cJSON_CreateArray();
    cJSON_AddItemToObject(root, "services", services);

    //封装三色灯
    lamp = cJSON_CreateObject();
    cJSON_AddStringToObject(lamp, "service_id", "lamp");
    properties = cJSON_CreateObject();
    cJSON_AddStringToObject(properties, "state",  globals_dev_status.lamp_state.lamp_power ? "off" : "on");
    cJSON_AddNumberToObject(properties, "strength",  globals_dev_status.lamp_state.strength);
    cJSON_AddNumberToObject(properties, "timer",  globals_dev_status.lamp_state.timer);
    RGB_color = cJSON_CreateObject();
    cJSON_AddNumberToObject(RGB_color, "R",  globals_dev_status.lamp_state.RGB_color_r);
    cJSON_AddNumberToObject(RGB_color, "G",  globals_dev_status.lamp_state.RGB_color_g);
    cJSON_AddNumberToObject(RGB_color, "B",  globals_dev_status.lamp_state.RGB_color_b);
    cJSON_AddItemToObject(properties, "RGB_color", RGB_color);
    cJSON_AddItemToObject(lamp, "properties", properties);
    cJSON_AddItemToArray(services, lamp);  // 将对象添加到数组中

    //封装蜂鸣器
    beep = cJSON_CreateObject();
    cJSON_AddStringToObject(beep, "service_id", "beep");
    properties = cJSON_CreateObject();
    cJSON_AddStringToObject(properties, "state",  globals_dev_status.beep_state.beep_power ? "off" : "on");
    cJSON_AddNumberToObject(properties, "frequency",  globals_dev_status.beep_state.frequency);
    cJSON_AddNumberToObject(properties, "timer",  globals_dev_status.beep_state.timer);
    cJSON_AddItemToObject(beep, "properties", properties);
    cJSON_AddItemToArray(services, beep);

    //封装风扇
    fan = cJSON_CreateObject();
    cJSON_AddStringToObject(fan, "service_id", "fan");
    properties = cJSON_CreateObject();
    cJSON_AddStringToObject(properties, "state",  globals_dev_status.fan_state.fan_power ? "off" : "on");
    cJSON_AddNumberToObject(properties, "speed",  globals_dev_status.fan_state.speed);
    cJSON_AddNumberToObject(properties, "timer",  globals_dev_status.fan_state.timer);
    cJSON_AddItemToObject(fan, "properties", properties);
    cJSON_AddItemToArray(services, fan);

    //封装环境传感器数据
    environment = cJSON_CreateObject();
    cJSON_AddStringToObject(environment, "service_id", "environment");
    properties = cJSON_CreateObject();
    cJSON_AddNumberToObject(properties, "light",  globals_dev_status.light);
    cJSON_AddNumberToObject(properties, "temperature",  globals_dev_status.temperature);
    cJSON_AddNumberToObject(properties, "humidity",  globals_dev_status.humidity);
    cJSON_AddNumberToObject(properties, "infrared",  globals_dev_status.infrared);
    cJSON_AddItemToObject(environment, "properties", properties);
    cJSON_AddItemToArray(services, environment);    

    /* 格式化打印创建的带数组的JSON对象 */
    char *str_print = cJSON_PrintUnformatted(root);
    if (str_print != NULL) {
        // printf("%s\n", str_print);
        if (strcpy_s(data, strlen(str_print) + 1, str_print) == 0) {
            ret = 0;
        } else {
            ret = -1;
        }
        cJSON_free(str_print);
    } else {
        ret = -1;
    }
    if (root != NULL) {
        cJSON_Delete(root);
    } else {
        ret = -1;
    }
    properties = str_print = root = lamp = beep = fan = environment = services = RGB_color = NULL;

    return ret;
}


/**
 * @brief 解析下发的命令
 * @note 更新全局变量中的各个外设及传感器状态
 * @param {*}
 * @return 0：成功 -1：失败
 */
int Json_Par_Data(char *data)
{
    cJSON *root = NULL, *command_name = NULL, *paras = NULL, *state = NULL;
    cJSON *red = NULL, *green = NULL, *blue = NULL;
    int ret_code = 0, dev_state = POWER_STATUS_OFF;
    root = cJSON_Parse((const char *)data);
    if (root) {
        // 解析JSON数据
        command_name = cJSON_GetObjectItem(root, "command_name");
        paras = cJSON_GetObjectItem(root, "paras");
        if (paras) {
            state = cJSON_GetObjectItem(paras, "state");
            if(!strcmp(state->valuestring, "on"))
                dev_state = POWER_STATUS_ON;
        }else
            ret_code = -1;

        if (command_name) {
            if (!strcmp(command_name->valuestring, "lamp")) {
                globals_dev_status.lamp_state.lamp_power = dev_state;
            } else if (!strcmp(command_name->valuestring, "fan")) {
                globals_dev_status.fan_state.fan_power = dev_state;
            } else if (!strcmp(command_name->valuestring, "beep")) {
                globals_dev_status.beep_state.beep_power = dev_state;
            } 
        }else
            ret_code = -1;
    }else
        ret_code = -1;
    cJSON_Delete(root);
    root = command_name = paras = state = red = green = blue = NULL;
    
    return ret_code;
}

